import { http } from "@/utils/http";
import { baseUrlApi } from "./utils";

export type UserResult = {
  success: boolean;
  data: {
    /** 用户名 */
    username: string;
    /** 当前登陆用户的角色 */
    roles: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
  errMsg: "";
};

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
  errMsg: "";
};

export type DataResult = {
  success: boolean;
  data?: Array<any>;
  errMsg: "";
};

export type StudentResult = {
  success: boolean;
  data?: {
    id: string;
    username: string;
    fullname: string;
    phone: string;
    idcard: string;
    gender: number;
    employment: string;
    verifyCode: string;
  };
  errMsg: "";
};

export type SiteResult = {
  success: boolean;
  data?: {
    id: string;
    name: string;
    site: string;
    api: string;
    operation1: string;
    operation2: number;
  };
  errMsg: "";
};

/** 注册 */
export const registerApi = (data?: object) => {
  return http.request<DataResult>("post", baseUrlApi("register"), { data });
};

/** 发送验证码 */
export const sendVerifyCodeApi = (data?: object) => {
  return http.request<DataResult>("post", baseUrlApi("sendVerifyCode"), {
    data
  });
};

/** 登录 */
export const getLoginAccount = (data?: object) => {
  return http.request<UserResult>("post", baseUrlApi("loginAccount"), { data });
};

export const getLoginPhone = (data?: object) => {
  return http.request<UserResult>("post", baseUrlApi("loginPhone"), { data });
};

/** 刷新token */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", baseUrlApi("refreshToken"), {
    data
  });
};

/** 获取学员信息 */
export const getStudentInto = () => {
  return http.request<StudentResult>("get", baseUrlApi("getStudentInto"));
};

export const updateStudentIntoByUser = (data?: object) => {
  return http.request<DataResult>(
    "post",
    baseUrlApi("updateStudentIntoByUser"),
    {
      data
    }
  );
};
/** 获取网站信息 */
export const getSysXtSite = () => {
  return http.request<SiteResult>("get", baseUrlApi("getSysXtSite"));
};
